﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Web.Domain.Abstract;
using Web.Domain.Entities;

namespace Web.Domain.Concrete
{
    public class EFComponentRepository : IComponentRepository
    {
        private EFDbContext context = new EFDbContext();
        public IQueryable<Component> Components
        {
            get
            {
                return context.Components;
            }
        }
        public bool SaveCompo(Component compo)
        {
            if (compo.id == 0)
            {
                context.Components.Add(compo);
            }
            else
            {
                Component dbEntry = context.Components.Find(compo.id);
                if (dbEntry != null)
                {
                    dbEntry.Name = compo.Name;
                    dbEntry.Ctrl = compo.Ctrl;
                }
            }
            if (context.SaveChanges() != 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public Component DeleteCompo(int id)
        {
            Component dbEntry = context.Components.Find(id);
            if (dbEntry != null)
            {
                context.Components.Remove(dbEntry);
                context.SaveChanges();
            }
            return dbEntry;
        }
    }
}
